﻿function getChartCounter()
{
    if (typeof chart == "undefined")
    {
        console.log("ChartCounter undef return 0");
        return 0;
    }
    else
    {
        console.log("ChartCounter " + ChartCounter);
        return ChartCounter;
    }
}


function poll()
{
    setTimeout(function () {
        $.ajax({
            type: 'POST',
            url: "Monitoring.aspx/GetNextResult"+"?&preventCache="+new Date().getTime(),
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ counter: getChartCounter(), id: window.location.hash.toString() }),
            cache: false,
            dataType: 'json',
            success: function (response)
            {
                console.log("success");

                if (response.d != null)
                {
                    document.getElementById('splashScreenOverlay').style.visibility = 'hidden';

                    var Result = JSON.parse(response.d);

                    UpdateUI(Result);

                    if (Result.JobFinished)
                    {
                        SetUIFinished();
                    }
                    else
                    {
                        poll();
                    }
                }
                else
                {
                    poll();
                }
            },
            error: function (xhr, textStatus, err)
            {
                console.log("readyState: " + xhr.readyState);
                console.log("responseText: " + xhr.responseText);
                console.log("status: " + xhr.status);
                console.log("text status: " + textStatus);
                console.log("error: " + err);

                poll();
            }
        });
    }, 1000);
};